package com.chenglu.android.goodsinfo.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chenglu.android.R;
import com.chenglu.android.common.utils.DateUtils;
import com.chenglu.android.common.utils.GlideUtils;
import com.chenglu.android.common.utils.LogUtils;
import com.chenglu.android.goodsinfo.bean.GoodsCommentBean;
import com.chenglu.android.goodsinfo.touch.BigImgActivity;


import java.util.ArrayList;
import java.util.List;

/**
 * @author: lyj
 * @Created: by Copyright 2023/3/13 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 * @Description：首页详情商品推荐的数据
 */
public class CommentAdapter extends BaseQuickAdapter<GoodsCommentBean.Data.Records, BaseViewHolder> {

    private Context context ;
    public CommentAdapter(@Nullable Context context, List<GoodsCommentBean.Data.Records> data) {
        super(R.layout.item_home_detail_commment, data);
        this.context = context;

    }




    @Override
    protected void convert(@NonNull BaseViewHolder helper, GoodsCommentBean.Data.Records item) {

        if(item!=null){
            TextView  tvDetailDes = helper.getView(R.id.item_tv_detail_des);
            TextView tvDetaalShopName = helper.getView(R.id.item_tv_detaal_shopname);
            ImageView imageView = helper.getView(R.id.item_iv_detail_shop);
            TextView tvCreatetime = helper.getView(R.id.item_createtime);

            RatingBar ratingBar = helper.getView(R.id.ratingBar);
            ratingBar.setRating(item.getGoodsReview());


            tvCreatetime.setText(item.getReviewTime());
            if(TextUtils.isEmpty(item.getUserNickName())){
                tvDetaalShopName.setText("匿名");
            }else {
                tvDetaalShopName.setText(item.getUserNickName());
            }

            tvDetailDes.setText(item.getReviewCntent());
            GlideUtils.setImageView(context,item.getUserAvatar(),imageView,30);

            List<String> picInfoBeanList = new ArrayList<>();
            RecyclerView rvCommentPic = helper.getView(R.id.rv_comment_pic);

            CommentItemPicAdapter picAdapter = new CommentItemPicAdapter(context,picInfoBeanList);
            rvCommentPic.setLayoutManager(new GridLayoutManager(context, 3,
                    LinearLayoutManager.VERTICAL, false));
            rvCommentPic.setAdapter(picAdapter);

            picAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    Intent intent = new Intent(context, BigImgActivity.class);
                    intent.putStringArrayListExtra("imgData", (ArrayList<String>) picInfoBeanList);
                    intent.putExtra("clickPosition", position);
                    context.startActivity(intent);
                }
            });

            if( item.getReviewImgList()!= null && !item.getReviewImgList().contains(",")){

                LogUtils.v("走iv");
                rvCommentPic.setVisibility(View.VISIBLE);
                picInfoBeanList.add(item.getReviewImgList());
                picAdapter.notifyDataSetChanged();
//                if(TextUtils.isEmpty(item.getReviewImgList())){
//                    rvCommentPic.setVisibility(View.GONE);
//                }


            }else if(item.getReviewImgList()!= null && item.getReviewImgList().contains(",")){
                LogUtils.v("走rv!!!" +picInfoBeanList.size());
                rvCommentPic.setVisibility(View.VISIBLE);

                String[] picArray = item.getReviewImgList().split(",");
                for (int i = 0; i <picArray.length ; i++) {
                    picInfoBeanList.add(picArray[i]);
                }
                picAdapter.notifyDataSetChanged();

            } else {
                LogUtils.v("走到这里~~~");
                rvCommentPic.setVisibility(View.GONE);
            }

        }

    }
}
